home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1099 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.0 KB

  1. Path: news.mira.net.au!news
  2. From: davidw@werple.net.au (David White)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Code loses this pointer value
  5. Date: 9 Jan 1996 20:02:06 +1100
  6. Organization: Werple Internet, Melbourne
  7. Message-ID: <4ctaue$c8l@werple.net.au>
  8. References: <1996Jan9.021817.24635@jarvis.cs.toronto.edu>
  9. NNTP-Posting-Host: werple.mira.net.au
  10.  
  11. victorng@dgp.toronto.edu (Victor Ng-Thow-Hing) writes:
  12.  
  13. >Hi everyone, 
  14.  
  15. >I have a particularly nasty bug in my C++ class code. Basically, the code
  16. >loses track of the this pointer value that points to the object that is
  17. >calling a member function. I have a class called bsplinevolume. The
  18. >member function "build_Controlpoints" calls another member function
  19. >"get_Skeleton_samp" as in the following short code snippet:
  20.  
  21. >void bsplinevolume::build_Controlpoints()
  22. >{
  23. >     <stuff deleted>
  24.  
  25. >     // Assign internal control points and interior cap ends.
  26. >     double point[3];
  27. >     for (int i=0; i<(N_s1-1); i++)
  28. >         for (int j=0; j < N_s2; j++)
  29. >             for (int k=0; k < N_s3; k++) {
  30. >                  get_Skeleton_samp(i,j,k,point); // member function
  31. >                  set_Controlpoint(i,j,k,point);  // member function
  32. >             }
  33.  
  34. >     <stuff deleted>
  35. >}
  36.  
  37. >When I run dbx on this code, if I type: print *this while in
  38. >build_Controlpoints, I get the proper 'this' value:
  39.  
  40. >(dbx) print *this
  41. >class bsplinevolume {
  42. >    N1 = 0x10004858
  43. >    N2 = 0x10004908
  44. >    N3 = 0x100049c8
  45. >    Axis = 0x10004398
  46. >    N_axis = 0
  47. >    Cap1 = 0x100044c0
  48. >    N_cap1 = 0
  49. >    Cap2 = 0x10004730
  50. >    N_cap2 = 0
  51. >    Skeleton_samps = 0x10004a78
  52. >    N_s1 = 10
  53. >    N_s2 = 10
  54. >    N_s3 = 10
  55. >    Controlpoints = 0x1000a840
  56. >    N_cpts = 1000
  57. >} 
  58.  
  59. >NOW, when I print *this while in get_Skeleton_samp, I get:
  60.  
  61. >(dbx) print *this
  62. ><no such address> 
  63.  
  64. >and consequently all the variables in this method are full of garbage.
  65. >You can see that 'this' has lost or corrupted its pointer value.      
  66.  
  67. >I'm using the CC SGI c++ compiler on an Indigo2 Extreme.
  68.  
  69. >Does anyone have any experience with this bug?
  70.  
  71. There does not appear to be enough information here to solve this 
  72. problem. There is nothing apparent to me that is wrong with this code, 
  73. but you haven't included the code for the function in which 'this' 
  74. becomes garbage. Possibilities include:
  75.     One of the functions you are calling is writing beyond the end
  76.     of a variable it has a pointer to.
  77.     Your print function is corrupting the object.
  78.  
  79. Have a close look at all the writes in the functions you are calling. Try 
  80. printing 'this' in lots of places (just the pointer value should do, not 
  81. the whole object) to narrow it down. Print 'this' immediately before and 
  82. after each function you call to isolate the function etc., etc.
  83. In most cases, a repeatable bug is not hard to find by a process of 
  84. elimination. Keep in mind though, that each function gets its own copy of 
  85. 'this', so just because its own 'this' is okay, it doesn't mean it hasn't 
  86. corrupted the 'this' in a calling function.
  87.  
  88. David White
  89. davidw@werple.mira.net.au
  90.  
  91.  
  92.